package com.itic.mall.mobile.banner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.RandomStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.mobile.index.ProductAndJd;
import com.itic.mall.system.banner.persistence.model.Banner;
import com.itic.mall.system.banner.persistence.model.BannerExample;
import com.itic.mall.system.banner.service.BannerService;
import com.itic.mall.system.product.persistence.model.Product;
import com.itic.mall.system.product.service.ProductService;
import com.itic.mall.system.productjd.persistence.model.ProductJd;


/**
 * 友惠家商城中心接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/banner")
public class MallBannerInterface
     extends BaseMobileInterface {
   @Autowired
   private ProductService productService;
   @Autowired
   private BannerService bannerService;
   
   
   /**
    * APP用户登入商城中心首页
    * @return
    */
   @RequestMapping(value="/list")
   @ResponseBody
   public Map<String,Object> list( HttpServletRequest request,HttpServletResponse response,String type){
	    Map<String, Object> resultMap = new HashMap<String,Object>();
	   	String result = FAIL;
	   	String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	   	try{
	   		
	   		BannerExample example = new BannerExample();
	   		example.setOrderByClause("REORDER ASC");
	   		com.itic.mall.system.banner.persistence.model.BannerExample.Criteria criteria = example.createCriteria();
	   		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
	   		if( StringHelper.isNotBlank(type) ){
	   			criteria.andTypeEqualTo(type);
	   		}
	   		
	   		List<Banner> banners = bannerService.list(example);
	   		
	   		resultMap.put(DATA, banners);
	   		result = SUCCESS;
	   		errorCode = SUCCESS_ERROR_CODE;
	       	errorMsg = SUCCESS_ERROR_MSG;
	   		
	   	}catch(Exception e){
	   		errorCode = SERVER_ERROR_CODE;
	       	errorMsg = SERVER_ERROR_MSG;
	   		logException("MallIndexInterface", "list", e.toString(),"商品搜索",e);
			
	   	}
	   	resultMap.put(RESULT, result);
	   	resultMap.put(ERROR_CODE, errorCode);
	   	resultMap.put(ERROR_MSG, errorMsg);
	   	isCrrossDomain(response);
	   	return resultMap;
   }
   
   /**
    * APP用户登入商城中心首页
    * @return
    */
   @RequestMapping(value="/detail")
   @ResponseBody
   public Map<String,Object> detail( HttpServletRequest request,HttpServletResponse response,String id){
	    Map<String, Object> resultMap = new HashMap<String,Object>();
	   	String result = FAIL;
	   	String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	   	try{
	   		
	   		if( StringHelper.isNotBlank(id) ){
	   			Map<String, Object> dataMap = new HashMap<String,Object>();
	   			Banner banner = bannerService.get(id);
	   			
	   			dataMap.put("title", banner.getTitle());
	   			dataMap.put("content", banner.getContent());
	   			dataMap.put("images", banner.getImages());
	   			
	   			List<ProductAndJd> projds = new ArrayList<ProductAndJd>();
	   			String productIds = banner.getProductIds();
	   			if( StringHelper.isNotBlank(productIds) ){
	   				String[] split = productIds.split(",");
		   			if( split.length>0 ){
		   				for(int i=0;i<split.length;i++ ){
		   					String idtype = split[i];
		   					String[] split2 = idtype.split(":");
		   					if( "001".equals(split2[1]) ){
		   						Product temp = productService.get(split2[0]); 
		   						if( null != temp ){
		   							ProductAndJd projd = new ProductAndJd();
			   		   				projd.setId(temp.getId());
			   		   				projd.setMainImageUrl(temp.getMainImageUrl());
			   		   				projd.setName(temp.getName());
			   		   				projd.setProductHongbao(temp.getProductHongbao());
			   		   				projd.setProductOrginPrice(temp.getProductOrginPrice());
			   		   				projd.setProductPrice(temp.getProductPrice());
			   		   				projd.setTotalFeedback(temp.getTotalFeedback());
			   		   				projd.setTotalSales(temp.getTotalSales());
			   		   				projd.setType("001");
			   		   				projd.setMarketProfit(temp.getMarketProfit());
			   		   				projd.setMarketProfitPercent(temp.getMarketProfitPercent());
			   		   				projds.add(projd);
		   						}
		   					}
		   					if( "002".equals(split2[1]) ){
		   						ProductJd temp = productJdService.get(split2[0]);
		   						if( null != temp ){
		   							ProductAndJd projd = new ProductAndJd();
			   		   				projd.setId(temp.getId());
			   		   				projd.setMainImageUrl(temp.getGoodsUrl());
			   		   				projd.setName(temp.getGoodsName());
			   		   				projd.setProductHongbao(temp.getConTicketPrice());
			   		   				projd.setProductOrginPrice(temp.getOriPrice());
			   		   				projd.setProductPrice(temp.getConPrice());
			   		   				projd.setTotalFeedback(RandomStringHelper.randomNumStr(3));
			   		   				projd.setTotalSales(temp.getTotalSales());
			   		   				projd.setType("002");
				   		   			projd.setMarketProfit(temp.getCommissionProfit());
			   		   				projd.setMarketProfitPercent(temp.getCommission());
			   		   				projds.add(projd);
		   						}
		   					}
		   					
		   				}
		   			}
		   			
		   			dataMap.put("products", projds);
		   			
		   			resultMap.put(DATA, dataMap);
			   		result = SUCCESS;
			   		errorCode = SUCCESS_ERROR_CODE;
			       	errorMsg = SUCCESS_ERROR_MSG;
	   			}else{
	   				result = FAIL;
			   		errorCode = PRODUCT_NULL_ERROR_CODE;
			       	errorMsg = PRODUCT_NULL_ERROR_MSG;
	   			}
	   		}
	   		
	   	}catch(Exception e){
	   		errorCode = SERVER_ERROR_CODE;
	       	errorMsg = SERVER_ERROR_MSG;
	   		logException("MallIndexInterface", "list", e.toString(),"商品搜索",e);
			
	   	}
	   	resultMap.put(RESULT, result);
	   	resultMap.put(ERROR_CODE, errorCode);
	   	resultMap.put(ERROR_MSG, errorMsg);
	   	isCrrossDomain(response);
	   	return resultMap;
   }
   
    
}